﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PusherRESTDotNet;

namespace WebApplication7
{
    public class Class1
    {
        ServiceNimic nimic = ServiceNimic.getInstance();
        String[] players = { "player1", "player2","player3","player4" };
        PusherProvider provider;
        int [] playerPositions = new int [4];
        Field [] fields = new Field [40];

        public void run()
        {
            // Inicijaliziranje na polinja prv red
            fields [0] = new Field (32,14);
            fields [1] = new Field (105,14);
            fields [2] = new Field (160,14);
            fields [3] = new Field (215,14);
            fields [4] = new Field (275,14);
            fields [5] = new Field (330,14);
            fields [6] = new Field (390,14);
            fields [7] = new Field (448,14);
            fields [8] = new Field (506,14);
            fields [9] = new Field (562,14);
            fields [10] = new Field (640,14);

            //Inicijaliziranje na polinja vtor red
            fields [11] = new Field (660,78);
            fields [12] = new Field (660,138);
            fields [13] = new Field (660,192);
            fields [14] = new Field (660,254);
            fields [15] = new Field (660,308);
            fields [16] = new Field (660,362);
            fields [17] = new Field (660,416);
            fields [18] = new Field (660,470);
            fields [19] = new Field (660,524);
            fields [20] = new Field (660,624);

            //Inicijaliziranje na polinja od tret red
            fields [21] = new Field (582,624);
            fields [22] = new Field (526,624);
            fields [23] = new Field (468,624);
            fields [24] = new Field (410,624);
            fields [25] = new Field (350,624);
            fields [26] = new Field (295,624);
            fields [27] = new Field (235,624);
            fields [28] = new Field (180,624);
            fields [29] = new Field (125,624);
            fields [30] = new Field (32,624);
            
            //Inicijaliziranje na polinja od cetvrti red
            fields [31] = new Field (32,524);
            fields [32] = new Field (32,470);
            fields [33] = new Field (32,416);
            fields [34] = new Field (32,362);
            fields [35] = new Field (32,308);
            fields [36] = new Field (32,254);
            fields [37] = new Field (32,192);
            fields [38] = new Field (32,138);
            fields [39] = new Field (32,78);
            
            provider = new PusherProvider("12015", "a1b663c201ac667143ce", "7533f72c70431ac5c53f");
            int playerCounter = 0;
            while (true)
            {
                var request = new ObjectPusherRequest("broadcast", "ask_roll_dice", players[playerCounter]);
                provider.Trigger(request);
                int br = 0;
                int kocka = 0;
                while (((kocka = nimic.getTheDiceNumber(playerCounter)) == 0) && (br < 60))
                {
                    System.Threading.Thread.Sleep(1000);
                    br++;
                }
                if (br < 60)
                {
                    request = new ObjectPusherRequest("broadcast", "info", "Player " + players[playerCounter] + " rolled "+kocka+"!");
                    provider.Trigger(request);
                    moveIt(playerCounter, kocka);
                }
                else
                {
                    request = new ObjectPusherRequest("broadcast", "info", "Player " + players[playerCounter] + " did not rolled the dice. It skips this turn.");
                    provider.Trigger(request);
                }
                playerCounter = (playerCounter + 1) % 4;
            }
        }


        private void moveIt(int indexPlayer, int howManyFields)
        {
            for (int i = 0; i < howManyFields; i++)
            {
                makeAMove(indexPlayer);
                System.Threading.Thread.Sleep(500);
            }
            
        }

        private void makeAMove(int indexPlayer)
        {
            ServiceNimic sn = ServiceNimic.getInstance();
            playerPositions[indexPlayer] = (playerPositions[indexPlayer] + 1) % 40;
            sn.setAPF(playerPositions[indexPlayer]);
            sn.setActivePlayer(players[indexPlayer]);
            var request = new ObjectPusherRequest("broadcast", "moveIt", "{\"player\":\"" + players[indexPlayer] + "\", \"left\":"+fields[playerPositions[indexPlayer]].getLeft()+", \"top\":"+fields[playerPositions[indexPlayer]].getTop()+"}");
            provider.Trigger(request);
            
            // tuka treba proverka dali nadminuva eden krug za da se isprati soopstenie za dodatni pari
        }
    }
}